package com.daidai.activitys

import android.graphics.Color
import com.daidai.base.BaseActivity
import com.daidai.spinner.R
import com.daidai.tools.G
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.formatter.IFillFormatter
import java.util.*

class LineChartActivity : BaseActivity() {

    lateinit var chart1: LineChart

    override fun getContentViewResId() = R.layout.activity_linechart

    override fun initView() {
        chart1 = findViewById(R.id.chart1)
    }

    override fun initData() {
        setData(50)
        chart1.invalidate()
    }

    private fun setData(count: Int) {
        val values = ArrayList<Entry>()
        for (i in 0 until count) {
            val value = G.randomInt(100,10000)
            values.add(Entry(i.toFloat(), value.toFloat()))
        }
        val set1: LineDataSet
        if (chart1.data != null &&
                chart1.data.dataSetCount > 0) {
            set1 = chart1.data.getDataSetByIndex(0) as LineDataSet
            set1.values = values
            chart1.data.notifyDataChanged()
            chart1.notifyDataSetChanged()
        } else {
            // create a dataset and give it a type
            set1 = LineDataSet(values, "步数")
            set1.mode = LineDataSet.Mode.LINEAR
            set1.cubicIntensity = 0.2f
            set1.setDrawFilled(true)
            set1.setDrawCircles(true)
            set1.lineWidth = 2f
            set1.circleRadius = 4f
            set1.setCircleColor(Color.YELLOW)
            set1.highLightColor = Color.rgb(244, 117, 117)
            set1.color = Color.GREEN
            set1.fillColor = Color.RED
            set1.fillAlpha = 100
            set1.setDrawHorizontalHighlightIndicator(false)
            set1.fillFormatter = IFillFormatter { _, _ -> chart1.axisLeft.axisMinimum }

            // create a data object with the data sets
            val data = LineData(set1)
//            data.setValueTypeface(tfLight)
            data.setValueTextSize(9f)
            data.setDrawValues(false)

            // set data
            chart1.data = data
        }
    }
}